package com.zl.cms.dao;

import com.google.common.base.Optional;
import com.zl.basic.dao.BaseDao;
import com.zl.basic.model.Pager;
import com.zl.cms.model.Keyword;
import com.zl.cms.model.Topic;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * User: zl email:c3tc3tc3t@126.com Date: 14-3-7 Time: 下午9:55
 */
@Repository("topicDao")
public class TopicDao extends BaseDao<Topic> implements ITopicDao {
  public static final Logger topicLogger = Logger.getLogger(TopicDao.class);

  @Override
  public Pager<Topic> find(Integer cid, String title, Integer status) {
    return this.find(null, cid, title, status);
  }


  private String getTopicSelect() {
    return "select new Topic(t.id,t.title,t.keyword,t.status,t.recommend,t.publishDate,t.cname,t.author) ";
  }

  @Override
  public Pager<Topic> find(Integer uid, Integer cid, String title, Integer status) {
    // 　Optional.fromNullable(T)：将一个T的实例转换为Optional对象，T的实例可以不为空，也可以为空[Optional.fromNullable(null)，和Optional.absent()等价。
    Optional<Integer> uidOpt = Optional.fromNullable(uid);
    Optional<Integer> cidOpt = Optional.fromNullable(cid);
    Optional<String> titleOpt = Optional.fromNullable(title);
    Optional<Integer> statusOpt = Optional.fromNullable(status);

    StringBuilder hql = new StringBuilder(getTopicSelect() + " from Topic t where 1=1 ");

    List<Object> params = new ArrayList<>();
    // boolean isPresent()：如果Optional包含的T实例不为null，则返回true；若T实例为null，返回false
    if (titleOpt.isPresent()) {
      hql.append(" and t.title like ?");
      // T get()：返回Optional包含的T实例，该T实例必须不为空；否则，对包含null的Optional实例调用get()会抛出一个IllegalStateException异常
      String param = "%" + titleOpt.get() + "%";
      params.add(param);
    }

    if (statusOpt.isPresent()) {
      hql.append(" and t.status = ?");
      params.add(statusOpt.get());
    }

    if (uidOpt.isPresent()) {
      hql.append(" and t.user.id = ?");
      params.add(uidOpt.get());
    }

    if (cidOpt.isPresent()) {
		if(cid != 0) {
			hql.append(" and t.channel.id = ?");
			params.add(cidOpt.get());
		}

    }

    String excutehql = hql.toString();

    Object[] os = new Object[params.size()];
    for (int i = 0; i < params.size(); i++) {
      os[i] = params.get(i);
    }

    topicLogger.debug(excutehql);
    return this.find(excutehql, os);
  }

  @Override
  public Pager<Topic> searchTopicByKeyword(String keyword) {
    String likeparam = "%" + keyword + "%";
    String hql = getTopicSelect() + " from Topic t where t.status=1 and t.keyword like ? ";
    return this.find(hql, likeparam);
  }

  @Override
  public Pager<Topic> searchTopic(String con) {
    String conparam = "%" + con + "%";
    String hql =
        getTopicSelect()
            + " from Topic t where t.status=1 and (t.title like ? or t.content like ? or t.summary like ? ) ";
    return this.find(hql, new Object[] {conparam, conparam, conparam});
  }

  @Override
  public Pager<Topic> findRecommendTopic(Integer cid) {
    Optional<Integer> cidOpt = Optional.fromNullable(cid);
    String hql = getTopicSelect() + " from Topic  t where t.status=1 and t.recommend = 1";
    if (!cidOpt.isPresent()) {
      return this.find(hql);
    } else {
      hql += " and t.channel.id = ? ";
      return this.find(hql, cid);
    }
  }


}
